package oa.hleast.xswl.service.studies.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.xswl.mapper.UmsUserMapper;
import oa.hleast.xswl.mapper.studies.CaucusOrganizationApplicationMapper;
import oa.hleast.xswl.mapper.studies.CaucusOrganizationMapper;
import oa.hleast.xswl.mapper.studies.CaucusOrganizationMemberMapper;
import oa.hleast.xswl.mapper.studies.ResearchActivitiesMapper;
import oa.hleast.xswl.pojo.domain.UmsMember;
import oa.hleast.xswl.pojo.domain.studies.CaucusOrganization;
import oa.hleast.xswl.pojo.domain.studies.CaucusOrganizationApplication;
import oa.hleast.xswl.pojo.domain.studies.CaucusOrganizationMember;
import oa.hleast.xswl.pojo.domain.studies.ResearchActivities;
import oa.hleast.xswl.service.studies.service.CaucusOrganizationApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 团党组织申请表(CaucusOrganizationApplication)表服务实现类
 *
 * @author makejava
 * @since 2021-11-08 13:33:55
 */
@AllArgsConstructor
@Transactional
@Slf4j
@Service("caucusOrganizationApplicationService")
public class CaucusOrganizationApplicationServiceImpl extends ServiceImpl<CaucusOrganizationApplicationMapper, CaucusOrganizationApplication> implements CaucusOrganizationApplicationService {

    private final CaucusOrganizationApplicationMapper caucusOrganizationApplicationDao;

    private final CaucusOrganizationMapper caucusOrganizationDao;

    private final CaucusOrganizationMemberMapper caucusOrganizationMemberDao;

    private final ResearchActivitiesMapper researchActivitiesDao;

    private final UmsUserMapper umsUserMapper;

    /**
     * 新增团党组织
     * @param caucusOrganizationApplication
     * @return
     */
    @Override
    public boolean addQuestionnaire(CaucusOrganizationApplication caucusOrganizationApplication) {
        if(caucusOrganizationApplication==null){
            return false;
        }

        int insert = caucusOrganizationApplicationDao.insert(caucusOrganizationApplication);
        if(insert==1){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 修改团党组织
     * @param caucusOrganization
     * @return
     */
    @Override
    public boolean updateOrganization(CaucusOrganization caucusOrganization) {
        if(caucusOrganization==null){
            return false;
        }

        int updateById = caucusOrganizationDao.updateById(caucusOrganization);
        if(updateById==1){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 删除团党组织
     * @param caucusOrganization
     * @return
     */
    @Override
    public boolean deleteOrganization(CaucusOrganization caucusOrganization) {
        if(caucusOrganization==null){
            return false;
        }

        int deleteById = caucusOrganizationDao.deleteById(caucusOrganization);
        if(deleteById==1){
            researchActivitiesDao.delete(new LambdaQueryWrapper<ResearchActivities>()
                    .eq(ResearchActivities::getCaucusOrganizationId,caucusOrganization.getId()));
            caucusOrganizationMemberDao.delete(new LambdaQueryWrapper<CaucusOrganizationMember>()
                    .eq(CaucusOrganizationMember::getCaucusOrganizationId,caucusOrganization.getId()));
            return true;
        }else {
            return false;
        }
    }

    /**
     * 手动添加团党组织成员
     * @param caucusOrganizationMember
     * @return
     */
    @Override
    public boolean manuallyAddOrganizationMember(CaucusOrganizationMember caucusOrganizationMember) {
        if(caucusOrganizationMember==null){
            return false;
        }

        String idCardNum = caucusOrganizationMember.getIdCardNum();
        UmsMember umsMember = umsUserMapper.selectOne(new LambdaQueryWrapper<UmsMember>()
                .eq(UmsMember::getIdCardNum, idCardNum));
        //审核通过
        caucusOrganizationMember.setCertification(1);
        caucusOrganizationMember.setUmsMemberId(umsMember.getId());
        caucusOrganizationMember.setNickname(umsMember.getNickname());

        int insert = caucusOrganizationMemberDao.insert(caucusOrganizationMember);
        if (insert==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updateOrganizationMember(CaucusOrganizationMember caucusOrganizationMember) {
        if(caucusOrganizationMember==null){
            return false;
        }

        int updateById = caucusOrganizationMemberDao.updateById(caucusOrganizationMember);
        if(updateById==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteOrganizationMember(String ids) {
        if(ids==null){
            return false;
        }

        List<String> idString = Arrays.asList(ids.split(","));
        for (String id : idString) {
            int delete = caucusOrganizationMemberDao.delete(new LambdaQueryWrapper<CaucusOrganizationMember>()
                    .eq(CaucusOrganizationMember::getId, id));
            if(delete==0){
                return false;
            }
        }
        return true;
    }
}

